package com.love.travel.service.impl;

import java.util.List;

import com.love.common.core.constant.SecurityConstants;
import com.love.common.core.domain.R;
import com.love.common.core.utils.DateUtils;
import com.love.common.core.utils.snow.SnowflakeIdGenerator;
import com.love.common.core.web.domain.AjaxResult;
import com.love.common.security.utils.SecurityUtils;
import com.love.system.api.RemoteUserService;
import com.love.system.api.model.LoginUser;
import com.love.travel.domain.TravelProductTag;
import com.love.travel.mapper.TravelProductTagMapper;
import com.love.usedcar.api.RemoteCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.love.travel.mapper.TravelProductMapper;
import com.love.travel.domain.TravelProduct;
import com.love.travel.service.ITravelProductService;

/**
 * 旅游产品Service业务层处理
 * 
 * @author love
 * @date 2025-10-22
 */
@Service
public class TravelProductServiceImpl implements ITravelProductService 
{
    @Autowired
    private TravelProductMapper travelProductMapper;
    @Autowired
    private RemoteCityService remoteCityService;
    @Autowired
    private TravelProductTagMapper travelProductTagMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    //引用雪花算法
    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(3, 3);

    /**
     * 查询旅游产品
     * 
     * @param productId 旅游产品主键
     * @return 旅游产品
     */
    @Override
    public TravelProduct selectTravelProductByProductId(Long productId)
    {
        return travelProductMapper.selectTravelProductByProductId(productId);
    }

    @Override
    public List<TravelProduct> selectTravelProductByUserId() {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid();
        return travelProductMapper.selectTravelProductByUserId(userId);
    }

    /**
     * 查询旅游产品列表
     * 
     * @param travelProduct 旅游产品
     * @return 旅游产品
     */
    @Override
    public List<TravelProduct> selectTravelProductList(TravelProduct travelProduct)
    {
        return travelProductMapper.selectTravelProductList(travelProduct);
    }

    /**
     * 新增旅游产品
     * 
     * @param travelProduct 旅游产品
     * @return 结果
     */
    @Override
    public int insertTravelProduct(TravelProduct travelProduct)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        // 查询用户信息
        Long userId = currentUser.getUserid(); // 获取用户ID
        R<LoginUser> sysUser = remoteUserService.userId(userId, SecurityConstants.INNER);
        String nickName = sysUser.getData().getSysUser().getNickName();
        travelProduct.setVendorId(userId);
        travelProduct.setVendorName(nickName);
        travelProduct.setProductId(idGenerator.nextId());
        if(travelProduct.getTagIds()!=null){
            TravelProductTag travelProductTag =  new TravelProductTag();
            travelProductTag.setProductId(travelProduct.getProductId());
            travelProductTag.setCreateTime(DateUtils.getNowDate());
            for(Long tagId:travelProduct.getTagIds()){
                travelProductTag.setTagId(tagId);
                this.travelProductTagMapper.insertTravelProductTag(travelProductTag);
            }
        }
        travelProduct.setCreateTime(DateUtils.getNowDate());
        return travelProductMapper.insertTravelProduct(travelProduct);
    }

    /**
     * 修改旅游产品
     * 
     * @param travelProduct 旅游产品
     * @return 结果
     */
    @Override
    public int updateTravelProduct(TravelProduct travelProduct)
    {
        if(travelProduct.getTagIds()!=null){
            this.travelProductTagMapper.deleteTravelProductTagByProductId(travelProduct.getProductId());
            TravelProductTag travelProductTag =  new TravelProductTag();
            travelProductTag.setProductId(travelProduct.getProductId());
            travelProductTag.setCreateTime(DateUtils.getNowDate());
            for(Long tagId:travelProduct.getTagIds()){
                travelProductTag.setTagId(tagId);
                this.travelProductTagMapper.insertTravelProductTag(travelProductTag);
            }
        }
        travelProduct.setUpdateTime(DateUtils.getNowDate());
        return travelProductMapper.updateTravelProduct(travelProduct);
    }

    /**
     * 批量删除旅游产品
     * 
     * @param productIds 需要删除的旅游产品主键
     * @return 结果
     */
    @Override
    public int deleteTravelProductByProductIds(Long[] productIds)
    {
        return travelProductMapper.deleteTravelProductByProductIds(productIds);
    }

    /**
     * 删除旅游产品信息
     * 
     * @param productId 旅游产品主键
     * @return 结果
     */
    @Override
    public int deleteTravelProductByProductId(Long productId)
    {
        return travelProductMapper.deleteTravelProductByProductId(productId);
    }

    @Override
    public AjaxResult selectCitys() {
        return remoteCityService.cascader(SecurityConstants.INNER);
    }
}
